"""
照明检测相关的数据模型
"""
from pydantic import BaseModel, Field
from typing import List, Dict, Optional, Tuple, Any
from datetime import datetime
from enum import Enum


class LightingStatus(str, Enum):
    """照明状态枚举"""
    ON = "on"
    OFF = "off"
    DIMMED = "dimmed"
    UNKNOWN = "unknown"


class LightingType(str, Enum):
    """照明类型枚举"""
    FLUORESCENT = "fluorescent"  # 荧光灯
    LED = "led"  # LED灯
    INCANDESCENT = "incandescent"  # 白炽灯
    NATURAL = "natural"  # 自然光
    MIXED = "mixed"  # 混合光源


class LightingAnomalyType(str, Enum):
    """照明异常类型枚举"""
    FLICKERING = "flickering"  # 闪烁
    OVERBRIGHTNESS = "overbrightness"  # 过亮
    UNDERBRIGHTNESS = "underbrightness"  # 过暗
    COLOR_SHIFT = "color_shift"  # 色温偏移
    UNEVEN_DISTRIBUTION = "uneven_distribution"  # 分布不均
    FAILURE = "failure"  # 故障


class LightingDetectionRequest(BaseModel):
    """照明检测请求"""
    camera_id: str = Field(..., description="摄像头ID")
    zone_id: str = Field(..., description="区域ID")
    image_data: Optional[str] = Field(None, description="Base64编码的图像数据")


class LightingDetectionResponse(BaseModel):
    """照明检测响应"""
    detection_id: str = Field(..., description="检测ID")
    camera_id: str = Field(..., description="摄像头ID")
    zone_id: str = Field(..., description="区域ID")
    timestamp: datetime = Field(..., description="检测时间")
    lighting_status: LightingStatus = Field(..., description="照明状态")
    brightness_level: float = Field(..., ge=0, le=1, description="亮度水平(0-1)")
    natural_light_level: float = Field(..., ge=0, le=1, description="自然光水平(0-1)")
    artificial_light_level: float = Field(..., ge=0, le=1, description="人工光水平(0-1)")
    lighting_type: LightingType = Field(..., description="照明类型")
    color_temperature: float = Field(..., ge=1000, le=10000, description="色温(K)")
    uniformity_score: float = Field(..., ge=0, le=1, description="均匀性评分(0-1)")
    detected_anomalies: List[Dict[str, Any]] = Field(default_factory=list, description="检测到的异常")
    energy_efficiency_score: float = Field(..., ge=0, le=100, description="能效评分(0-100)")
    recommendations: List[str] = Field(default_factory=list, description="改进建议")


class LightingAnomalyResponse(BaseModel):
    """照明异常响应"""
    anomaly_id: str = Field(..., description="异常ID")
    zone_id: str = Field(..., description="区域ID")
    camera_id: str = Field(..., description="摄像头ID")
    anomaly_type: LightingAnomalyType = Field(..., description="异常类型")
    severity: str = Field(..., description="严重程度")
    description: str = Field(..., description="异常描述")
    detected_at: datetime = Field(..., description="检测时间")
    confidence: float = Field(..., ge=0, le=1, description="置信度(0-1)")
    location: Optional[Tuple[int, int]] = Field(None, description="异常位置坐标")


class LightingZoneConfigRequest(BaseModel):
    """照明区域配置请求"""
    zone_id: str = Field(..., description="区域ID")
    zone_name: str = Field(..., description="区域名称")
    zone_type: str = Field(..., description="区域类型")
    optimal_brightness_range: Tuple[float, float] = Field(..., description="最佳亮度范围")
    optimal_color_temp_range: Tuple[float, float] = Field(..., description="最佳色温范围")
    natural_light_weight: float = Field(0.3, ge=0, le=1, description="自然光权重")
    energy_saving_mode: bool = Field(True, description="是否启用节能模式")
    operating_hours: Dict[str, Tuple[str, str]] = Field(..., description="工作时间")


class LightingZoneConfigResponse(BaseModel):
    """照明区域配置响应"""
    zone_id: str = Field(..., description="区域ID")
    zone_name: str = Field(..., description="区域名称")
    zone_type: str = Field(..., description="区域类型")
    optimal_brightness_range: Tuple[float, float] = Field(..., description="最佳亮度范围")
    optimal_color_temp_range: Tuple[float, float] = Field(..., description="最佳色温范围")
    natural_light_weight: float = Field(..., description="自然光权重")
    energy_saving_mode: bool = Field(..., description="是否启用节能模式")
    operating_hours: Dict[str, Tuple[str, str]] = Field(..., description="工作时间")


class LightingStatsResponse(BaseModel):
    """照明统计响应"""
    zone_id: str = Field(..., description="区域ID")
    total_detections: int = Field(..., description="总检测次数")
    average_brightness: float = Field(..., description="平均亮度")
    average_efficiency: float = Field(..., description="平均能效评分")
    average_natural_light: float = Field(..., description="平均自然光水平")
    average_uniformity: float = Field(..., description="平均均匀性")
    lighting_status_distribution: Dict[str, int] = Field(..., description="照明状态分布")
    anomaly_count: int = Field(..., description="异常数量")
    period_days: int = Field(..., description="统计周期(天)")


class LightingServiceStatsResponse(BaseModel):
    """照明服务统计响应"""
    total_detections: int = Field(..., description="总检测次数")
    anomalies_detected: int = Field(..., description="检测到的异常数量")
    average_brightness: float = Field(..., description="平均亮度")
    average_efficiency_score: float = Field(..., description="平均能效评分")
    energy_savings_potential: float = Field(..., description="节能潜力百分比")


class LightingDetectionListRequest(BaseModel):
    """照明检测列表请求"""
    zone_id: Optional[str] = Field(None, description="区域ID")
    start_time: Optional[datetime] = Field(None, description="开始时间")
    end_time: Optional[datetime] = Field(None, description="结束时间")
    limit: int = Field(100, ge=1, le=1000, description="返回数量限制")


class LightingAnomalyListRequest(BaseModel):
    """照明异常列表请求"""
    zone_id: Optional[str] = Field(None, description="区域ID")
    anomaly_type: Optional[LightingAnomalyType] = Field(None, description="异常类型")
    severity: Optional[str] = Field(None, description="严重程度")
    limit: int = Field(100, ge=1, le=1000, description="返回数量限制")


class LightingZoneStatsRequest(BaseModel):
    """照明区域统计请求"""
    zone_id: str = Field(..., description="区域ID")
    days: int = Field(7, ge=1, le=365, description="统计天数")


# 响应包装器
class LightingDetectionListResponse(BaseModel):
    """照明检测列表响应"""
    detections: List[LightingDetectionResponse] = Field(..., description="检测结果列表")
    total: int = Field(..., description="总数量")


class LightingAnomalyListResponse(BaseModel):
    """照明异常列表响应"""
    anomalies: List[LightingAnomalyResponse] = Field(..., description="异常列表")
    total: int = Field(..., description="总数量")


class LightingZoneConfigListResponse(BaseModel):
    """照明区域配置列表响应"""
    configs: List[LightingZoneConfigResponse] = Field(..., description="配置列表")
    total: int = Field(..., description="总数量")